package srv

import (
	"fmt"
	"jgt-wallet/src/bll"
	"jgt-wallet/src/context"
	"jgt-wallet/src/schema"

	"github.com/spf13/viper"
)

// Demo 示例接口
type AlipayApp struct {
	AliAppBll *bll.AlipayApp `inject:""`
	OrderBll  *bll.Order     `inject:""`
}

// 支付宝应用支付接口
func (a *AlipayApp) Pay(ctx *context.Context) error {
	var (
		key    string
		param  = new(schema.AliAppParams)
		err    error
		result map[string]string
	)
	err = ctx.ParseJSON(param)
	if err != nil {
		return ctx.ResErrBadRequest(err)
	}

	key = ctx.Param("key")
	param.Url = fmt.Sprintf("%s/api/v1/public/alipay/callback/app/%s", viper.GetString("web_addr"), key)
	result, err = a.AliAppBll.Pay(key, param)
	if err != nil {
		ctx.ResErrBadRequest(err)
	}

	return ctx.ResSuccess(result)
}

// 支付宝应用同步回调接口
func (a *AlipayApp) Check(ctx *context.Context) error {
	var (
		key    string
		param  = new(schema.AliAppCheckParams)
		result *schema.AliAppCheckResult
		err    error
	)
	err = ctx.ParseJSON(param)
	if err != nil {
		return ctx.ResErrBadRequest(err)
	}
	key = ctx.Param("key")
	result, err = a.AliAppBll.Check(key, param)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResSuccess(result)
}

// 检查订单支付结果
func (a *AlipayApp) CheckResult(ctx *context.Context) error {

	return ctx.ResOK()
}
